package io.jpress.addon.interact.directive;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.JbootControllerContext;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jboot.web.directive.base.PaginateDirectiveBase;
import io.jpress.JPressOptions;
import io.jpress.addon.interact.model.JpressAddonInteract;
import io.jpress.addon.interact.model.JpressAddonInteractComment;
import io.jpress.addon.interact.service.JpressAddonInteractCommentService;
import io.jpress.addon.interact.service.JpressAddonInteractService;
import io.jpress.commons.directive.DirectveKit;
import org.checkerframework.checker.units.qual.C;

import javax.servlet.http.HttpServletRequest;


@JFinalDirective("interactCommentList")
public class InteractCommentDirective extends JbootDirectiveBase {

    @Inject
    private JpressAddonInteractCommentService commentService;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {

        Controller controller = JbootControllerContext.get();
        int page = controller.getParaToInt(1, 1);
        int pageSize = getPara("pageSize", scope,10);
        int interact_id = getPara("interact_id", scope,0);
        Columns columns = new Columns();
        columns.eq("interact_id",interact_id);
        Page<JpressAddonInteractComment> paginate = commentService.paginateByColumns(page, pageSize,columns,"create_time asc");
        scope.setLocal("interactCommentList", paginate);
        renderBody(env, scope, writer);

    }


    @JFinalDirective("interactCommentListPaginate")
    public static class TemplatePaginateDirective extends PaginateDirectiveBase {

        @Override
        protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) {
            HttpServletRequest request = JbootControllerContext.get().getRequest();
            String url = request.getRequestURI();
            String contextPath = JFinal.me().getContextPath();

            boolean firstGotoIndex = getPara("firstGotoIndex", scope, false);

            if (pageNumber == 1 && firstGotoIndex) {
                return contextPath + "/";
            }

            // 如果当前页面是首页的话
            // 需要改变url的值，因为 上一页或下一页是通过当前的url解析出来的
            if (url.equals(contextPath + "/")) {
                url = contextPath + "/interactCommentList/index"
                        + JPressOptions.getAppUrlSuffix();
            }
            return DirectveKit.replacePageNumber(url, pageNumber);
        }

        @Override
        protected Page<?> getPage(Env env, Scope scope, Writer writer) {
            return  (Page<?>) scope.get("interactCommentList");
        }
    }

        @Override
    public boolean hasEnd() {
        return true;
    }
}
